/*******************************************************************************
 *	                           bsp cpu.c
 *******************************************************************************/
#include "bsp.h"
#include "mytype.h"
#include "str_ext.h"
#include "dig_encode.h"
#include "sys_init.h"
#include "dig_encode.h"
#include "pincfg.h"
#include "common.h"
#include "os_obj.h"

sig_encode_type sigobj, sigobj2;

static void tx_out1(uint8_t st)
{
	sig1.out(st);
}
static uint8_t rx_in1(void)
{
	return sig1.in_st();
}
static void io_dir1(uint8_t dir) // 0r1w
{
	if (dir)
		sig1.dir_out_pp();
	else
		sig1.dir_in_up();
}

static void tx_out2(uint8_t st)
{
	sig2.out(st);
}
static uint8_t rx_in2(void)
{
	return sig2.in_st();
}
static void io_dir2(uint8_t dir) // 0r1w
{
	if (dir)
		sig2.dir_out_pp();
	else
		sig2.dir_in_up();
}

void sig_timer(void)
{
	sig_encode_handle(&sigobj);
	sig_encode_handle(&sigobj2);
}

obj_init_order5(sig_init);
void sig_init(void)
{
	sigobj.tx_out = tx_out1;
	sigobj.rx_in = rx_in1;
	sigobj.io_dir = io_dir1;
	sigobj.bit_sv = 32;

	sigobj2.tx_out = tx_out2;
	sigobj2.rx_in = rx_in2;
	sigobj2.io_dir = io_dir2;
	sigobj2.bit_sv = 32;

	sig2.dir_in_up();
	sig1.dir_in_up();

	app_timer_task_add(sig_timer);
}

int sig1_send(int argc, char **argv)
{
	int tmo = 30;
	uint32_t dat = 0x55AA;

	if (argc > 1)
		dat = atoi(argv[1]);

	sig_encode_write(&sigobj, dat);

	while (tmo--)
	{
		if (sigobj2.b_rxne)
		{
			dbg_print("sig2 rcv dat %d\n", sig_encode_read(&sigobj2));
		}
		osDelay(10);
	}
	return 0;
}
DBG_FUN_EXPORT(sig1_send);

int sig2_send(int argc, char **argv)
{
	int tmo = 30;
	uint32_t dat = 0x55AA;

	if (argc > 1)
		dat = atoi(argv[1]);

	sig_encode_write(&sigobj2, dat);

	while (tmo--)
	{
		if (sigobj.b_rxne)
		{
			dbg_print("sig1 rcv dat %d\n", sig_encode_read(&sigobj));
		}
		osDelay(10);
	}
	return 0;
}
DBG_FUN_EXPORT(sig2_send);
